package com.lvyq.myplayer.ui.fragment.singer

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.singer.SingerAlbumAdapter
import com.lvyq.myplayer.model.singer.SingerAlbumBean
import com.lvyq.myplayer.model.singer.SingerAlbumData
import com.lvyq.myplayer.parcelable.SongInfoData
import com.lvyq.myplayer.ui.activity.AudioPlayerActivity
import com.lvyq.myplayer.ui.activity.singer.SingerAlbumActivity
import com.lvyq.myplayer.util.NetworkUtils
import com.lvyq.myplayer.util.ThreadUtil
import com.lvyq.myplayer.util.URLProviderUtils

/**
 * 文件描述：SingerAlbumPagerFragment
 * 作者：lvyq
 * 创建时间：2025-04-25
 * 修改时间：2025-04-25
 */
class SingerAlbumPagerFragment(var singerId:Int):  Fragment() {


    lateinit var singerAlbumView: RecyclerView


    val singerAlbumAdapter by lazy { getAdapter() }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return initView()
    }

    private fun initView(): View? {
        var view  = View.inflate(context, R.layout.fragment_singer_album,null)
        singerAlbumView=view.findViewById(R.id.singerAlbumView)

        singerAlbumView.layoutManager = GridLayoutManager(context, 2)
        //适配
        singerAlbumView.adapter=singerAlbumAdapter
        initData()
        return view

    }

    private fun initData() {
        //不分页
        val url = URLProviderUtils.getSingerAlbumList(
            singerId)
        NetworkUtils.get(
            url = url,
            responseClass = SingerAlbumBean::class.java,
            successCallback = { pageData ->
                singerAlbumAdapter.initData(
                    ArrayList(pageData.rows)
                )
            },
            errorCallback = { errorMsg ->
                Toast.makeText(context, "加载失败：$errorMsg", Toast.LENGTH_SHORT).show()
                Log.e("SingerSongPagerFragment", "加载失败：$errorMsg")
            }
        )
    }

    fun getAdapter(): SingerAlbumAdapter {
        return SingerAlbumAdapter(context,object :
            SingerAlbumAdapter.OnItemClickListener{
            override fun onItemClick(position: Int, album: SingerAlbumData) {
                // 点击跳转播放页面,查询该音乐下的所属专辑下的所有歌
                println("点击了"+album.albumName)
                startAudioPlayActivity(album)
            }
        })
    }


    /**
     * 跳转到歌曲播放界面
     */
    fun  startAudioPlayActivity(data: SingerAlbumData){
        ThreadUtil.runOnMainThread({
            // 点击跳转播放页面
            var intent = Intent(context, SingerAlbumActivity::class.java)
            intent.putExtra("data",data)
            //跳转专辑页
            context?.let {
                it.startActivity(intent)
            }
        })
    }

}